package chloroform.util

import javax.servlet.http.HttpServletRequest

/**
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: 24.2.11
 * Time: 16:42
 * To change this template use File | Settings | File Templates.
 */

object RequestUtils {

  def resolveProjectAndPageName(request: HttpServletRequest) = {
    val steps: Array[String] = request.getRequestURI.split("\\/")
    if (steps.length < 2)
      throw new IllegalArgumentException("URI " +
        request.getRequestURI + " does not conform the format: 'project/' projectName '/' entryName")
    val reversed: Array[String] = steps.reverse
    val pageName = reversed.head
    val projectName = reversed.tail.head
    (projectName, pageName)
  }

  def resolvePageName(request: HttpServletRequest) = {
    val steps: Array[String] = request.getPathInfo.split("\\/")
    if (steps.length < 3)
      throw new IllegalArgumentException("URI " +
        request.getRequestURI + " does not conform the format: 'project/' projectName '/' entryName")

    steps(2)
  }

  def resolveProjectName(request: HttpServletRequest) = {
    val steps: Array[String] = request.getPathInfo.split("\\/")
    if (steps.length < 2)
      throw new IllegalArgumentException("URI " +
        request.getRequestURI + " does not conform the format: 'project/' projectName")

    steps(1)
  }

}